package com.backend.springboot_project_demo.entity;

import com.backend.springboot_project_demo.config.SimpleGrantedAuthorityDeserializer;
import com.backend.springboot_project_demo.utils.PasswordUtils;
import com.backend.springboot_project_demo.utils.UuidUtils;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserTransfer implements UserDetails, Serializable {
    private final static int NEGATIVE_ONE = -1;
    private final static int ZERO = 0;
    private final static int ONE = 1;
    private final static int TEN = 10;
    private final static String FEMALE = "女";
    private final static String MALE = "男";
    private final static String ACTIVE = "有效";
    private final static String INACTIVE = "失效";

    private String id;
    private String username;
    private String password;
    private String roleId;
    private @Setter String role;
    private @Setter List<String> permissions;
    private String sex;
    private String status;
    private String address;
    private String createTime;
    private String updateTime;
    private int page;
    private int size;
    private List<SimpleGrantedAuthority> authorities;
    private boolean enabled;
    private boolean accountNonLocked;
    private boolean accountNonExpired;
    private boolean credentialsNonExpired;

    public UserTransfer(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public UserTransfer(String id, String username, String password, String roleId, String sex, String status, String address, String createTime, String updateTime, int page, int size) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.roleId = roleId;
        this.sex = sex;
        this.status = status;
        this.address = address;
        this.createTime = createTime;
        this.updateTime = updateTime;
        this.page = page;
        this.size = size;
    }

    public User toAddUser() {
        return new User(
                UuidUtils.getRandomString(8),
                this.username,
                PasswordUtils.encode(this.password),
                "3",
                chooseSex(this.sex),
                chooseStatus(this.status),
                this.address,
                new Date().toString(),
                this.updateTime
        );
    }

    public User toUpdateUser() {
        return new User(
                this.id,
                this.username,
                PasswordUtils.encode(this.password),
                "3",
                chooseSex(this.sex),
                chooseStatus(this.status),
                this.address,
                this.createTime,
                new Date().toString()
        );
    }

    private int chooseSex(String sex) {
        if (MALE.equals(sex)) {
            return ZERO;
        } else if (FEMALE.equals(sex)) {
            return ONE;
        } else {
            return NEGATIVE_ONE;
        }
    }

    private int chooseStatus(String status) {
        if (ACTIVE.equals(status)) {
            return ZERO;
        } else if (INACTIVE.equals(status)) {
            return ONE;
        } else {
            return NEGATIVE_ONE;
        }
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (!Objects.isNull(this.authorities)) {
            return authorities;
        }
        return Objects.isNull(this.permissions) ? null : this.permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
